﻿using System;
using VegetableSale.NET.Dao;
using VegetableSale.NET.Entities;

namespace VegetableSale.NET.Services;

public class CustomerService
{
    private CustomerMapper customerMapper = new CustomerMapper();

    public Result getCustList(QueryDTO queryDTO)
    {
        CustomerPage customerPage = new CustomerPage();
        uint pageNo = queryDTO.PageNo;
        uint pageSize = queryDTO.PageSize;
        List<Customer> customerList = customerMapper.getCustList(queryDTO);
        List<Customer> returnCust = new List<Customer>();

        if (pageSize * pageNo - 1 >= customerList.Count)
        {
            for (uint i = pageSize * (pageNo - 1); i <= customerList.Count - 1; i++)
            {
                Customer customer = customerList[(int)i];
                returnCust.Add(customer);
            }
        }
        else
        {
            for (uint i = pageSize * (pageNo - 1); i <= pageSize * pageNo - 1; i++)
            {
                Customer customer = customerList[(int)i];
                returnCust.Add(customer);
            }
        }

        customerPage.Records = returnCust;
        customerPage.Pages = (int)pageNo;
        customerPage.Size = pageSize;
        customerPage.Total = (uint)customerList.Count;

        return new Result(200, "搜索成功", customerPage);
    }

    public Result addCustomer(QueryCustomer queryCustomer)
    {
        customerMapper.addCustomer(queryCustomer);
        return new Result(200, "操作成功", "");
    }

    public Result custdelete(int custid)
    {
        customerMapper.custdelete(custid);
        return new Result(200, "", 1);
    }

    public Result custupdate(Customer customer)
    {
        customerMapper.custupdate(customer);
        return new Result(200, "", 1);
    }

    public Result getAllCust()
    {
        return new Result(0, "操作成功", customerMapper.getALLCust());
    }
}

